package vn.vfriends.vbox.security;

import io.buji.pac4j.ClientRealm;
import java.util.List;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.subject.PrincipalCollection;
import vn.vfriends.vbox.model.Role;
import vn.vfriends.vbox.model.User;

/**
 *
 * @author tuancn
 */
public class VBoxClientRealm extends ClientRealm {
    
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(final PrincipalCollection principals) {
        // create simple authorization info
        final SimpleAuthorizationInfo simpleAuthorizationInfo = (SimpleAuthorizationInfo)super.doGetAuthorizationInfo(principals);
        // add default roles
        
        User user = (User)SecurityUtils.getSubject().getSession().getAttribute("LOGGED_USER");
        
        if (user != null) {
            List<Role> roles = user.getRoles();

            for (Role role : roles) {
                simpleAuthorizationInfo.getRoles().add(role.getCode().toString());
            }
        }
        // add default permissions
        //simpleAuthorizationInfo.addStringPermissions(split(this.defaultPermissions));
        return simpleAuthorizationInfo;
    }
}
